-- Create the addon
Fizzlebag = AceLibrary("AceAddon-2.0"):new("AceEvent-2.0", "AceHook-2.1", "AceDB-2.0", "AceConsole-2.0");

local containerMax = 15;
local bagMax = 30;
local bankMax = 40;
local _G = getfenv(0);

function Fizzlebag:OnInitialize()
	self:MakeBorders();
end

function Fizzlebag:OnEnable()
	self:SecureHook("ContainerFrame_OnShow", "UPDATE_ZHE_BAGS_CAUSE_SOMETHING_HAPPEND"); -- Bored, why yes!
	self:RegisterEvent("BAG_UPDATE", "UPDATE_ZHE_BAGS_CAUSE_SOMETHING_HAPPEND"); -- Bored, why yes!
	self:RegisterEvent("PLAYERBANKSLOTS_CHANGED", "UPDATE_ZHE_BAGS_CAUSE_SOMETHING_HAPPEND"); -- Bored, why yes!
	self:RegisterEvent("PLAYERBANKBAGSLOTS_CHANGED", "UPDATE_ZHE_BAGS_CAUSE_SOMETHING_HAPPEND"); -- Bored, why yes!
	self:MakeBorders();
end

function Fizzlebag:OnDisable()
	self:HideBorders();
end

function Fizzlebag:CreateBorder(slottype, slot, name)
	local gslot = _G[slottype..slot];
	if gslot then
		if (not getglobal(slottype .. slot .. name .. "B")) then
			-- Create border
			local border = gslot:CreateTexture(slottype .. slot .. name .. "B", "OVERLAY");
			border:SetTexture("Interface\\Buttons\\UI-ActionButton-Border");
			border:SetBlendMode("ADD");
			border:SetVertexColor(0, 1, 0);
			border:SetAlpha( (5/10) );
			border:SetHeight(68);
			border:SetWidth(68);
			border:SetPoint("CENTER", gslot, "CENTER", 0, 1);
			border:Hide();
		end
	end
end

function Fizzlebag:MakeBorders()
	for containerIndex = 1, containerMax do
		for itemIndex = 1, bagMax do
			self:CreateBorder("ContainerFrame"..containerIndex, "Item"..itemIndex, "Fizzlebag");
		end
	end
	
	for bankIndex = 1, bankMax do
		self:CreateBorder("BankFrame", "Item"..bankIndex, "Fizzlebag");
	end
end

function Fizzlebag:HideBorders()
	for containerIndex = 1, containerMax do
		for itemIndex = 1, bagMax do
			local borderFrame = _G["ContainerFrame"..containerIndex.."Item"..itemIndex.."FizzlebagB"];
			if (borderFrame) then borderFrame:Hide(); end
		end
	end
	
	for bankIndex = 1, bankMax do
		local borderFrame = _G["BankFrameItem"..bankIndex.."FizzlebagB"];
		if (borderFrame) then borderFrame:Hide(); end
	end
end

function Fizzlebag:UpdateItems()
	for containerIndex = 1, containerMax do
		local bagFrame = _G["ContainerFrame"..containerIndex];
		if (bagFrame) then
			local bagIndex = bagFrame:GetID();
			if (bagFrame:IsVisible()) then
				for itemIndex = 1, bagMax do
					local slotFrame = _G["ContainerFrame"..containerIndex.."Item"..itemIndex];
					if (slotFrame) then
						local slotIndex = abs(itemIndex - (GetContainerNumSlots(bagIndex) + 1));
						local borderFrame = _G["ContainerFrame"..containerIndex.."Item"..itemIndex.."FizzlebagB"];
						if (slotFrame:IsVisible()) then
							local ItemLink = GetContainerItemLink(bagIndex, slotIndex);
							if (ItemLink) then
								_, _, ItemRarity = GetItemInfo(ItemLink);
								if (ItemRarity) then
									local r, g, b, _ = GetItemQualityColor(ItemRarity);
									if (ItemRarity == 0) then r, g, b = (8/10), 0 , 0; end -- Coloring junk items red instead of grey to distinguish from white!
									borderFrame:SetVertexColor(r, g, b);
									borderFrame:Show();
								else
									borderFrame:Hide();
								end
							else
								borderFrame:Hide();
							end
						end
					end
				end
			end
		end
	end
	
	for bankIndex = 1, bankMax do
		local slotFrame = _G["BankFrameItem"..bankIndex];
		if ( (slotFrame) and (slotFrame:IsVisible()) ) then
			local borderFrame = _G["BankFrameItem"..bankIndex.."FizzlebagB"];
			local ItemLink = GetContainerItemLink(-1, bankIndex);
			if (ItemLink) then
				_, _, ItemRarity = GetItemInfo(ItemLink);
				if (ItemRarity) then
					local r, g, b, _ = GetItemQualityColor(ItemRarity);
					if (ItemRarity == 0) then r, g, b = (8/10), 0 , 0; end -- Coloring junk items red instead of grey to distinguish from white!
					borderFrame:SetVertexColor(r, g, b);
					borderFrame:Show();
				else
					borderFrame:Hide();
				end
			else
				borderFrame:Hide();
			end
		end
	end
end

function Fizzlebag:UPDATE_ZHE_BAGS_CAUSE_SOMETHING_HAPPEND()
	self:UpdateItems();
end